package service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.dao.DataAccessException;

import persistence.dao.TdfkfaBasicInvestDAO;
import persistence.dao.TdfkfaBasicScxmgkDAO;
import persistence.dao.TdfkfaBasicXmqntdphlxDAO;
import persistence.dao.TdfkfaBasicXmqtdlyxzDAO;
import persistence.dao.TdfkfaBasicYqfkmjDAO;
import persistence.dao.TdfkfaComposeUnitDAO;
import persistence.dao.TdfkfaExpertListDAO;
import persistence.dao.TdfkfaMainDAO;
import persistence.dao.TdfkfaPrjDxtpDAO;
import persistence.dao.TdfkfaPrjExpertsDAO;
import persistence.dao.TdfkfaPrjFafjDAO;
import persistence.dao.TdfkfaPrjPsyjDAO;
import persistence.dao.TdfkfaUserDAO;
import persistence.model.TdfkfaBasicInvest;
import persistence.model.TdfkfaBasicInvestExample;
import persistence.model.TdfkfaBasicScxmgk;
import persistence.model.TdfkfaBasicScxmgkExample;
import persistence.model.TdfkfaBasicXmqntdphlx;
import persistence.model.TdfkfaBasicXmqntdphlxExample;
import persistence.model.TdfkfaBasicXmqtdlyxz;
import persistence.model.TdfkfaBasicXmqtdlyxzExample;
import persistence.model.TdfkfaBasicYqfkmj;
import persistence.model.TdfkfaBasicYqfkmjExample;
import persistence.model.TdfkfaComposeUnit;
import persistence.model.TdfkfaComposeUnitExample;
import persistence.model.TdfkfaExpertList;
import persistence.model.TdfkfaExpertListExample;
import persistence.model.TdfkfaMain;
import persistence.model.TdfkfaMainExample;
import persistence.model.TdfkfaPrjDxtp;
import persistence.model.TdfkfaPrjDxtpExample;
import persistence.model.TdfkfaPrjExperts;
import persistence.model.TdfkfaPrjFafj;
import persistence.model.TdfkfaPrjFafjExample;
import persistence.model.TdfkfaPrjPsyj;
import persistence.model.TdfkfaPrjPsyjExample;
import persistence.model.TdfkfaUser;
import persistence.model.TdfkfaUserExample;
import persistence.model.TdfkfaMainExample.Criteria;
import presentation.extendmodel.PlanDetailModel;
import presentation.extendmodel.TdfkfaMainEx;
import presentation.extendmodel.TdfkfaPrjExpertsEx;
import presentation.queryvo.PlanListQueryVO;
import service.PlanService;
import util.StringUtil;
import constant.DictConstant;
import constant.ServiceConstants;

public class PlanServiceImpl implements PlanService {

    private TdfkfaMainDAO tdfkfaMainDAO;

    private TdfkfaBasicXmqntdphlxDAO tdfkfaBasicXmqntdphlxDAO;

    private TdfkfaBasicScxmgkDAO tdfkfaBasicScxmgkDAO;

    private TdfkfaBasicYqfkmjDAO tdfkfaBasicYqfkmjDAO;

    private TdfkfaBasicXmqtdlyxzDAO tdfkfaBasicXmqtdlyxzDAO;

    private TdfkfaBasicInvestDAO tdfkfaBasicInvestDAO;

    private TdfkfaComposeUnitDAO tdfkfaComposeUnitDAO;

    private TdfkfaPrjExpertsDAO tdfkfaPrjExpertsDAO;

    private TdfkfaPrjPsyjDAO tdfkfaPrjPsyjDAO;
    
    private TdfkfaPrjFafjDAO tdfkfaPrjFafjDAO;

    private TdfkfaPrjDxtpDAO tdfkfaPrjDxtpDAO;

    private TdfkfaUserDAO tdfkfaUserDAO;
    
    private TdfkfaExpertListDAO tdfkfaExpertListDAO;
    
    public List<TdfkfaMainEx> getPlanList(PlanListQueryVO queryVO) {

        TdfkfaMainExample example = new TdfkfaMainExample();
        example.createCriteria().andPrjScztcodeEqualTo(new Short(queryVO.getScztCode()));

        List<TdfkfaMainEx> planList = tdfkfaMainDAO.selectPlanByExample(example, 1, 10);

        return planList;
    }

    /**
     * 取得已提交方案的数据
     * 
     * @param queryVO
     * <br>
     *            包括：unit 单位<br>
     *            xzCode 性质<br>
     *            kzCode 矿种<br>
     *            scztCode 审查次数 "1":对应SCZT第一次未审; "2":对应SCZT第二次未审;
     *            "3":对应SCZT第三次未审
     * @return 已提交方案数据
     */
    public List<TdfkfaMainEx> getSubmittedPlanList(PlanListQueryVO queryVO, int pageIndex, int pageSize) {

        TdfkfaMainExample example = new TdfkfaMainExample();
        // 方案状态 2: 已提交
//        Criteria criteria = example.createCriteria().andPrjFaztcodeEqualTo(new Short(DictConstant.FAZT_SUBMITTED)).andPrjScztcodeEqualTo(new Short(queryVO.getScztCode()));
        Criteria criteria = example.createCriteria().andPrjScztcodeEqualTo(new Short(queryVO.getScztCode())).andPrjFaztcodeGreaterThanOrEqualTo(new Short(DictConstant.FAZT_EDITING)).andPrjFaztcodeLessThanOrEqualTo(new Short(DictConstant.FAZT_SUBMITTED));

        if (!StringUtil.isEmpty(queryVO.getUnit())) {
            criteria.andPrjUnitLike("%".concat(queryVO.getUnit()).concat("%"));
        }
        if (!StringUtil.isEmpty(queryVO.getXzCode())) {
            criteria.andPrjXzcodeEqualTo(new Short(queryVO.getXzCode()));
        }
        if (!StringUtil.isEmpty(queryVO.getKzCode())) {
            criteria.andPrjKzcodeEqualTo(new Short(queryVO.getKzCode()));
            Criteria criteria1 = example.createCriteria().andPrjKzcode1EqualTo(new Short(queryVO.getKzCode())).andPrjFaztcodeGreaterThanOrEqualTo(new Short(DictConstant.FAZT_EDITING)).andPrjFaztcodeLessThanOrEqualTo(new Short(DictConstant.FAZT_SUBMITTED)).andPrjScztcodeEqualTo(
                    new Short(queryVO.getScztCode()));
            Criteria criteria2 = example.createCriteria().andPrjKzcode2EqualTo(new Short(queryVO.getKzCode())).andPrjFaztcodeGreaterThanOrEqualTo(new Short(DictConstant.FAZT_EDITING)).andPrjFaztcodeLessThanOrEqualTo(new Short(DictConstant.FAZT_SUBMITTED)).andPrjScztcodeEqualTo(
                    new Short(queryVO.getScztCode()));
            Criteria criteria3 = example.createCriteria().andPrjKzcode3EqualTo(new Short(queryVO.getKzCode())).andPrjFaztcodeGreaterThanOrEqualTo(new Short(DictConstant.FAZT_EDITING)).andPrjFaztcodeLessThanOrEqualTo(new Short(DictConstant.FAZT_SUBMITTED)).andPrjScztcodeEqualTo(
                    new Short(queryVO.getScztCode()));
            example.or(criteria);
            example.or(criteria1);
            example.or(criteria2);
            example.or(criteria3);
        }
        example.setOrderByClause("Prj_FAZTCode desc, RegisterID desc");

        List<TdfkfaMainEx> planList = tdfkfaMainDAO.selectSubmittedPlanByExample(example, pageIndex, pageSize);

        return planList;
    }

    /**
     * 取得已提交方案的数量
     * 
     * @param queryVO
     * <br>
     *            包括：unit 单位<br>
     *            xzCode 性质<br>
     *            kzCode 矿种<br>
     *            scztCode 审查次数 "1":对应SCZT第一次未审; "2":对应SCZT第二次未审;
     *            "3":对应SCZT第三次未审
     * @return 已提交方案数量
     */
    public int countSubmittedPlan(PlanListQueryVO queryVO) {
        TdfkfaMainExample example = new TdfkfaMainExample();
        // 方案状态 2: 已提交
//        Criteria criteria = example.createCriteria().andPrjFaztcodeEqualTo(new Short(DictConstant.FAZT_SUBMITTED)).andPrjScztcodeEqualTo(new Short(queryVO.getScztCode()));
        Criteria criteria = example.createCriteria().andPrjScztcodeEqualTo(new Short(queryVO.getScztCode())).andPrjFaztcodeGreaterThanOrEqualTo(new Short(DictConstant.FAZT_EDITING)).andPrjFaztcodeLessThanOrEqualTo(new Short(DictConstant.FAZT_SUBMITTED));

        if (!StringUtil.isEmpty(queryVO.getUnit())) {
            criteria.andPrjUnitLike("%".concat(queryVO.getUnit()).concat("%"));
        }
        if (!StringUtil.isEmpty(queryVO.getXzCode())) {
            criteria.andPrjXzcodeEqualTo(new Short(queryVO.getXzCode()));
        }
        if (!StringUtil.isEmpty(queryVO.getKzCode())) {
            criteria.andPrjKzcodeEqualTo(new Short(queryVO.getKzCode()));
            Criteria criteria1 = example.createCriteria().andPrjKzcode1EqualTo(new Short(queryVO.getKzCode())).andPrjFaztcodeGreaterThanOrEqualTo(new Short(DictConstant.FAZT_EDITING)).andPrjFaztcodeLessThanOrEqualTo(new Short(DictConstant.FAZT_SUBMITTED)).andPrjScztcodeEqualTo(
                    new Short(queryVO.getScztCode()));
            Criteria criteria2 = example.createCriteria().andPrjKzcode2EqualTo(new Short(queryVO.getKzCode())).andPrjFaztcodeGreaterThanOrEqualTo(new Short(DictConstant.FAZT_EDITING)).andPrjFaztcodeLessThanOrEqualTo(new Short(DictConstant.FAZT_SUBMITTED)).andPrjScztcodeEqualTo(
                    new Short(queryVO.getScztCode()));
            Criteria criteria3 = example.createCriteria().andPrjKzcode3EqualTo(new Short(queryVO.getKzCode())).andPrjFaztcodeGreaterThanOrEqualTo(new Short(DictConstant.FAZT_EDITING)).andPrjFaztcodeLessThanOrEqualTo(new Short(DictConstant.FAZT_SUBMITTED)).andPrjScztcodeEqualTo(
                    new Short(queryVO.getScztCode()));
            example.or(criteria);
            example.or(criteria1);
            example.or(criteria2);
            example.or(criteria3);
        }

        int count = tdfkfaMainDAO.countByExample(example);

        return count;
    }

    /**
     * 取得送审方案的数据
     * 
     * @param queryVO
     * <br>
     *            包括：unit 单位<br>
     *            scztCode 审查次数 "1":对应SCZT第一次未审; "2":对应SCZT第二次未审;
     *            "3":对应SCZT第三次未审
     * @return 送审方案数据
     */
    public List<TdfkfaMainEx> getNeedJudgePlanList(PlanListQueryVO queryVO, int pageIndex, int pageSize) {
        
        TdfkfaMainExample example = new TdfkfaMainExample();
        // 方案状态 3: 已通过
        Criteria criteria = example.createCriteria().andPrjFaztcodeEqualTo(new Short(DictConstant.FAZT_PASSED)).andPrjScztcodeEqualTo(new Short(queryVO.getScztCode()));

        if (!StringUtil.isEmpty(queryVO.getUnit())) {
            criteria.andPrjUnitLike("%".concat(queryVO.getUnit()).concat("%"));
        }
//        if (!StringUtil.isEmpty(queryVO.getXzCode())) {
//            criteria.andPrjXzcodeEqualTo(new Short(queryVO.getXzCode()));
//        }
//        if (!StringUtil.isEmpty(queryVO.getKzCode())) {
//            criteria.andPrjKzcodeEqualTo(new Short(queryVO.getKzCode()));
//            Criteria criteria1 = example.createCriteria().andPrjKzcode1EqualTo(new Short(queryVO.getKzCode())).andPrjFaztcodeEqualTo(new Short(DictConstant.FAZT_PASSED)).andPrjScztcodeEqualTo(new Short(queryVO.getScztCode()));
//            Criteria criteria2 = example.createCriteria().andPrjKzcode2EqualTo(new Short(queryVO.getKzCode())).andPrjFaztcodeEqualTo(new Short(DictConstant.FAZT_PASSED)).andPrjScztcodeEqualTo(new Short(queryVO.getScztCode()));
//            Criteria criteria3 = example.createCriteria().andPrjKzcode3EqualTo(new Short(queryVO.getKzCode())).andPrjFaztcodeEqualTo(new Short(DictConstant.FAZT_PASSED)).andPrjScztcodeEqualTo(new Short(queryVO.getScztCode()));
//            example.or(criteria);
//            example.or(criteria1);
//            example.or(criteria2);
//            example.or(criteria3);
//        }
        
        example.setOrderByClause("RegisterID desc");

        List<TdfkfaMainEx> planList = tdfkfaMainDAO.selectNeedJudgePlanByExample(example, pageIndex, pageSize);

        return planList;
    }
    
    /**
     * 取得送审方案的数量
     * 
     * @param queryVO
     * <br>
     *            包括：unit 单位<br>
     *            scztCode 审查次数 "1":对应SCZT第一次未审; "2":对应SCZT第二次未审;
     *            "3":对应SCZT第三次未审
     * @return 送审方案数量
     */
    public int countNeedJudgePlan(PlanListQueryVO queryVO) {
        
        TdfkfaMainExample example = new TdfkfaMainExample();
        // 方案状态 3: 已通过
        Criteria criteria = example.createCriteria().andPrjFaztcodeEqualTo(new Short(DictConstant.FAZT_PASSED)).andPrjScztcodeEqualTo(new Short(queryVO.getScztCode()));

        if (!StringUtil.isEmpty(queryVO.getUnit())) {
            criteria.andPrjUnitLike("%".concat(queryVO.getUnit()).concat("%"));
        }
//        if (!StringUtil.isEmpty(queryVO.getXzCode())) {
//            criteria.andPrjXzcodeEqualTo(new Short(queryVO.getXzCode()));
//        }
//        if (!StringUtil.isEmpty(queryVO.getKzCode())) {
//            criteria.andPrjKzcodeEqualTo(new Short(queryVO.getKzCode()));
//            Criteria criteria1 = example.createCriteria().andPrjKzcode1EqualTo(new Short(queryVO.getKzCode())).andPrjFaztcodeEqualTo(new Short(DictConstant.FAZT_PASSED)).andPrjScztcodeEqualTo(new Short(queryVO.getScztCode()));
//            Criteria criteria2 = example.createCriteria().andPrjKzcode2EqualTo(new Short(queryVO.getKzCode())).andPrjFaztcodeEqualTo(new Short(DictConstant.FAZT_PASSED)).andPrjScztcodeEqualTo(new Short(queryVO.getScztCode()));
//            Criteria criteria3 = example.createCriteria().andPrjKzcode3EqualTo(new Short(queryVO.getKzCode())).andPrjFaztcodeEqualTo(new Short(DictConstant.FAZT_PASSED)).andPrjScztcodeEqualTo(new Short(queryVO.getScztCode()));
//            example.or(criteria);
//            example.or(criteria1);
//            example.or(criteria2);
//            example.or(criteria3);
//        }

        int count = tdfkfaMainDAO.countByExample(example);

        return count;
    }

    /**
     * 取得方案详细信息
     */
    public PlanDetailModel getPlanDetail(String planId) {

        TdfkfaMainExample example = new TdfkfaMainExample();
        example.createCriteria().andPrjPkidEqualTo(new Integer(planId));

        // 取得方案基本信息
        TdfkfaMainEx tdfkfaMainEx = tdfkfaMainDAO.selectSinglePlanByExample(example);

        // 取得项目区内土地破坏类型信息
        TdfkfaBasicXmqntdphlx xmqntdphlx = tdfkfaBasicXmqntdphlxDAO.selectByPrimaryKey(planId);

        // 取得生产项目概况
        TdfkfaBasicScxmgk scxmgk = tdfkfaBasicScxmgkDAO.selectByPrimaryKey(planId);

        // 取得预期复垦面积
        TdfkfaBasicYqfkmj yqfkmj = tdfkfaBasicYqfkmjDAO.selectByPrimaryKey(planId);

        // 取得项目区土地利用现状
        TdfkfaBasicXmqtdlyxz xmqtdlyxz = tdfkfaBasicXmqtdlyxzDAO.selectByPrimaryKey(planId);

        // 投资
        TdfkfaBasicInvest invest = tdfkfaBasicInvestDAO.selectByPrimaryKey(new Integer(planId));

        // 编制单位
        TdfkfaComposeUnit unit = tdfkfaComposeUnitDAO.selectByPrimaryKey(new Integer(planId));

        // 专家
        TdfkfaPrjExpertsEx experts = tdfkfaPrjExpertsDAO.selectExpertsByPrimaryKey(new Integer(planId));

        // 方案附件
        TdfkfaPrjFafjExample fafjExample = new TdfkfaPrjFafjExample();
        fafjExample.createCriteria().andPlanIdEqualTo(new Integer(planId));
        List<TdfkfaPrjFafj> fafjList = tdfkfaPrjFafjDAO.selectByExample(fafjExample);

        TdfkfaPrjDxtpExample dxtpExample = new TdfkfaPrjDxtpExample();
        dxtpExample.createCriteria().andPlanIdEqualTo(new Integer(planId));
        List<TdfkfaPrjDxtp> dxtpList = tdfkfaPrjDxtpDAO.selectByExample(dxtpExample);
        
        PlanDetailModel planDetail = new PlanDetailModel();
        planDetail.setMain(tdfkfaMainEx);
        planDetail.setXmqntdphlx(xmqntdphlx);
        planDetail.setScxmgk(scxmgk);
        planDetail.setYqfkmj(yqfkmj);
        planDetail.setXmqtdlyxz(xmqtdlyxz);
        planDetail.setInvest(invest);
        planDetail.setUnit(unit);
        planDetail.setExperts(experts);
        planDetail.setDxtpList(dxtpList);
        planDetail.setFafjList(fafjList);
        return planDetail;
    }
    
    /**
     * 获得项目基本信息
     * @param planId
     * @return
     */
    public TdfkfaMain getPlanMain(Integer planId) {
        TdfkfaMain main = tdfkfaMainDAO.selectByPrimaryKey(planId);
        return main;
    }
    
    /**
     * 获得破坏类型
     * @param planId
     * @return
     */
    public TdfkfaBasicXmqntdphlx getPlanPhlx(Integer planId) {
        TdfkfaBasicXmqntdphlx xmqntdphlx = tdfkfaBasicXmqntdphlxDAO.selectByPrimaryKey(planId.toString());
        return xmqntdphlx;
    }
    
    /**
     * 获得项目概况
     * @param planId
     * @return
     */
    public TdfkfaBasicScxmgk getPlanXmgk(Integer planId) {
        TdfkfaBasicScxmgk scxmgk = tdfkfaBasicScxmgkDAO.selectByPrimaryKey(planId.toString());
        return scxmgk;
    }

    /**
     * 获得复垦面积
     * @param planId
     * @return
     */
    public TdfkfaBasicYqfkmj getPlanFkmj(Integer planId) {
        TdfkfaBasicYqfkmj yqfkmj = tdfkfaBasicYqfkmjDAO.selectByPrimaryKey(planId.toString());
        return yqfkmj;
    }
    
    /**
     * 获得土地复垦投资
     * @param planId
     * @return
     */
    public TdfkfaBasicInvest getPlanFktz(Integer planId) {
        TdfkfaBasicInvest invest = tdfkfaBasicInvestDAO.selectByPrimaryKey(planId);
        return invest;
    }
    
    /**
     * 获得土地利用现状
     * @param planId
     * @return
     */
    public TdfkfaBasicXmqtdlyxz getPlanLyxz(Integer planId) {
        TdfkfaBasicXmqtdlyxz xmqtdlyxz = tdfkfaBasicXmqtdlyxzDAO.selectByPrimaryKey(planId.toString());
        return xmqtdlyxz;
    }
    
    /**
     * 通过提交的方案
     * 
     * @param planId
     *            方案索引
     * @return 修改记录数
     */
    public int passSubmittedPlan(String planId) {

        TdfkfaMain record = new TdfkfaMain();
        record.setPrjPkid(new Integer(planId));
        record.setPrjFaztcode(new Short(DictConstant.FAZT_PASSED));

        int updateNum = tdfkfaMainDAO.updateByPrimaryKeySelective(record);

        return updateNum;
    }

    /**
     * 返修提交的方案
     * 
     * @param planId
     *            方案索引
     * @return 修改记录数
     */
    public int rejectSubmittedPlan(String planId) {

        TdfkfaMain record = new TdfkfaMain();
        record.setPrjPkid(new Integer(planId));
        record.setPrjFaztcode(new Short(DictConstant.FAZT_EDITING));

        int updateNum = tdfkfaMainDAO.updateByPrimaryKeySelective(record);

        return updateNum;
    }

    /**
     * 删除提交的方案
     * 
     * @param planId
     *            方案索引
     * @return 修改记录数
     */
    public int deleteSubmittedPlan(String planId) {

        TdfkfaMain record = new TdfkfaMain();
        record.setPrjPkid(new Integer(planId));
        record.setPrjFaztcode(new Short(DictConstant.FAZT_DELETED));

        int updateNum = tdfkfaMainDAO.updateByPrimaryKeySelective(record);

        return updateNum;
    }

    /**
     * 取得已结束评审的数据
     * 
     * @return 已结束评审方案数据
     */
    public List<TdfkfaMainEx> getJudgedPlanList(PlanListQueryVO queryVO, int pageIndex, int pageSize) {

        TdfkfaMainExample example = new TdfkfaMainExample();
        Criteria criteria = example.createCriteria().andPrjFaztcodeGreaterThanOrEqualTo(new Short(DictConstant.FAZT_JUDGED));

        if (!StringUtil.isEmpty(queryVO.getUnit())) {
            criteria.andPrjUnitLike("%".concat(queryVO.getUnit()).concat("%"));
        }
        if (!StringUtil.isEmpty(queryVO.getXzCode())) {
            criteria.andPrjXzcodeEqualTo(new Short(queryVO.getXzCode()));
        }
        if (!StringUtil.isEmpty(queryVO.getKzCode())) {
            criteria.andPrjKzcodeEqualTo(new Short(queryVO.getKzCode()));
            Criteria criteria1 = example.createCriteria().andPrjKzcode1EqualTo(new Short(queryVO.getKzCode())).andPrjFaztcodeGreaterThanOrEqualTo(new Short(DictConstant.FAZT_JUDGED));
            Criteria criteria2 = example.createCriteria().andPrjKzcode2EqualTo(new Short(queryVO.getKzCode())).andPrjFaztcodeGreaterThanOrEqualTo(new Short(DictConstant.FAZT_JUDGED));
            Criteria criteria3 = example.createCriteria().andPrjKzcode3EqualTo(new Short(queryVO.getKzCode())).andPrjFaztcodeGreaterThanOrEqualTo(new Short(DictConstant.FAZT_JUDGED));
            example.or(criteria);
            example.or(criteria1);
            example.or(criteria2);
            example.or(criteria3);
        }
        if (!StringUtil.isEmpty(queryVO.getOrderType())) {
            example.setOrderByClause(queryVO.getOrderType());
        }

        List<TdfkfaMainEx> planList = tdfkfaMainDAO.selectJudgedPlanByExample(example, pageIndex, pageSize);

        return planList;
    }

    /**
     * 取得已结束评审的数量
     * 
     * @return 已结束评审方案数量
     */
    public int countJudgedPlan(PlanListQueryVO queryVO) {

        TdfkfaMainExample example = new TdfkfaMainExample();
        Criteria criteria = example.createCriteria().andPrjFaztcodeGreaterThanOrEqualTo(new Short(DictConstant.FAZT_JUDGED));

        if (!StringUtil.isEmpty(queryVO.getUnit())) {
            criteria.andPrjUnitLike("%".concat(queryVO.getUnit()).concat("%"));
        }
        if (!StringUtil.isEmpty(queryVO.getXzCode())) {
            criteria.andPrjXzcodeEqualTo(new Short(queryVO.getXzCode()));
        }
        if (!StringUtil.isEmpty(queryVO.getKzCode())) {
            criteria.andPrjKzcodeEqualTo(new Short(queryVO.getKzCode()));
            Criteria criteria1 = example.createCriteria().andPrjKzcode1EqualTo(new Short(queryVO.getKzCode())).andPrjFaztcodeGreaterThanOrEqualTo(new Short(DictConstant.FAZT_JUDGED));
            Criteria criteria2 = example.createCriteria().andPrjKzcode2EqualTo(new Short(queryVO.getKzCode())).andPrjFaztcodeGreaterThanOrEqualTo(new Short(DictConstant.FAZT_JUDGED));
            Criteria criteria3 = example.createCriteria().andPrjKzcode3EqualTo(new Short(queryVO.getKzCode())).andPrjFaztcodeGreaterThanOrEqualTo(new Short(DictConstant.FAZT_JUDGED));
            example.or(criteria);
            example.or(criteria1);
            example.or(criteria2);
            example.or(criteria3);
        }

        int count = tdfkfaMainDAO.countByExample(example);

        return count;
    }

    /**
     * 取得评审中方案
     * @param queryVO
     * @param intPageIndex
     * @param intPageSize
     * @return 评审中方案
     */
    public List<TdfkfaMainEx> getJudgingPlanList(PlanListQueryVO queryVO, int pageIndex, int pageSize) {
        TdfkfaMainExample example = new TdfkfaMainExample();
        Criteria criteria = example.createCriteria().andPrjFaztcodeEqualTo(new Short(DictConstant.FAZT_JUDGING));

        if (!StringUtil.isEmpty(queryVO.getUnit())) {
            criteria.andPrjUnitLike("%".concat(queryVO.getUnit()).concat("%"));
        }
        if (!StringUtil.isEmpty(queryVO.getXzCode())) {
            criteria.andPrjXzcodeEqualTo(new Short(queryVO.getXzCode()));
        }
        if (!StringUtil.isEmpty(queryVO.getKzCode())) {
            criteria.andPrjKzcodeEqualTo(new Short(queryVO.getKzCode()));
            Criteria criteria1 = example.createCriteria().andPrjKzcode1EqualTo(new Short(queryVO.getKzCode())).andPrjFaztcodeEqualTo(new Short(DictConstant.FAZT_JUDGED));
            Criteria criteria2 = example.createCriteria().andPrjKzcode2EqualTo(new Short(queryVO.getKzCode())).andPrjFaztcodeEqualTo(new Short(DictConstant.FAZT_JUDGED));
            Criteria criteria3 = example.createCriteria().andPrjKzcode3EqualTo(new Short(queryVO.getKzCode())).andPrjFaztcodeEqualTo(new Short(DictConstant.FAZT_JUDGED));
            example.or(criteria);
            example.or(criteria1);
            example.or(criteria2);
            example.or(criteria3);
        }
        example.setOrderByClause("Prj_PKID desc");

        List<TdfkfaMainEx> planList = tdfkfaMainDAO.selectJudgingPlanByExample(example, pageIndex, pageSize);

        return planList;
    }

    /**
     * 取得评审中方案数量
     * @param queryVO
     * @return 评审中方案数量
     */
    public int countJudgingPlan(PlanListQueryVO queryVO) {
        TdfkfaMainExample example = new TdfkfaMainExample();
        Criteria criteria = example.createCriteria().andPrjFaztcodeEqualTo(new Short(DictConstant.FAZT_JUDGING));

        if (!StringUtil.isEmpty(queryVO.getUnit())) {
            criteria.andPrjUnitLike("%".concat(queryVO.getUnit()).concat("%"));
        }
        if (!StringUtil.isEmpty(queryVO.getXzCode())) {
            criteria.andPrjXzcodeEqualTo(new Short(queryVO.getXzCode()));
        }
        if (!StringUtil.isEmpty(queryVO.getKzCode())) {
            criteria.andPrjKzcodeEqualTo(new Short(queryVO.getKzCode()));
            Criteria criteria1 = example.createCriteria().andPrjKzcode1EqualTo(new Short(queryVO.getKzCode())).andPrjFaztcodeEqualTo(new Short(DictConstant.FAZT_JUDGED));
            Criteria criteria2 = example.createCriteria().andPrjKzcode2EqualTo(new Short(queryVO.getKzCode())).andPrjFaztcodeEqualTo(new Short(DictConstant.FAZT_JUDGED));
            Criteria criteria3 = example.createCriteria().andPrjKzcode3EqualTo(new Short(queryVO.getKzCode())).andPrjFaztcodeEqualTo(new Short(DictConstant.FAZT_JUDGED));
            example.or(criteria);
            example.or(criteria1);
            example.or(criteria2);
            example.or(criteria3);
        }

        int count = tdfkfaMainDAO.countByExample(example);

        return count;
    }

    /**
     * 取得评审意见数据
     * 
     * @param planId
     *            方案索引
     * @return 评审意见数据
     */
    public List<TdfkfaPrjPsyj> getPlanJudgeResult(String planId) {

    	TdfkfaPrjPsyjExample example = new TdfkfaPrjPsyjExample();
    	example.createCriteria().andPrjPkidEqualTo(new Integer(planId));
    	example.setOrderByClause("ExpertNo");
        List<TdfkfaPrjPsyj> resultList = tdfkfaPrjPsyjDAO.selectByExample(example);

        return resultList;
    }

    public int assignExpert(TdfkfaPrjExperts prjExpert) {
        
        try {
            tdfkfaPrjExpertsDAO.deleteByPrimaryKey(prjExpert.getPrjPkid());
            tdfkfaPrjExpertsDAO.insertSelective(prjExpert);
        } catch (DataAccessException e) {
            e.printStackTrace();
            return ServiceConstants.FAILURE;
        }
        return ServiceConstants.SUCCESS;
    }

    public TdfkfaPrjExpertsEx getAssignedExpert(String planId) {
        
        TdfkfaPrjExpertsEx experts = tdfkfaPrjExpertsDAO.selectExpertsByPrimaryKey(new Integer(planId));
        return experts;
    }

    public List<TdfkfaMainEx> getOwnJudgingPlanList(Integer expertId) {
        List<TdfkfaMainEx> list = tdfkfaMainDAO.selectOwnJudgingPlanListByExpertId(expertId);
        return list;
    }

    /**
     * 查看所有方案
     * 
     * @return
     */
    public List<TdfkfaMainEx> getAllPlanList(PlanListQueryVO queryVO, int pageIndex, int pageSize) {

        TdfkfaMainExample example = new TdfkfaMainExample();
        Criteria criteria = example.createCriteria();

        if (!StringUtil.isEmpty(queryVO.getUnit())) {
            criteria.andPrjUnitLike("%".concat(queryVO.getUnit()).concat("%"));
        }
        if (!StringUtil.isEmpty(queryVO.getOrderType())) {
            example.setOrderByClause(queryVO.getOrderType());
        }

        List<TdfkfaMainEx> list = tdfkfaMainDAO.selectPlanByExample(example, pageIndex, pageSize);
        return list;
    }
    
    /**
     * 查看所有方案数量
     * 
     * @return 所有方案数量
     */
    public int countAllPlan(PlanListQueryVO queryVO) {

        TdfkfaMainExample example = new TdfkfaMainExample();
        Criteria criteria = example.createCriteria();

        if (!StringUtil.isEmpty(queryVO.getUnit())) {
            criteria.andPrjUnitLike("%".concat(queryVO.getUnit()).concat("%"));
        }

        int count = tdfkfaMainDAO.countByExample(example);
        return count;
    }
    
    /**
     * 保存方案基本信息
     * @param main 方案基本信息
     * @return 成功返回方案ID；失败返回null
     */
    public Integer savePlanBasic(TdfkfaMain main) {

        main.setStateid(new Short("0"));
        main.setPrjFaztcode(new Short(DictConstant.FAZT_EDITING));
        main.setPrjScztcode(new Short("1"));

        Integer id = null;

        try {
            if (main.getPrjPkid() == null) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmm");
                main.setRegisterid(sdf.format(new Date()));
                id = tdfkfaMainDAO.insertSelectiveReturnPrimaryKey(main);
            } else {
                tdfkfaMainDAO.updateByPrimaryKeySelective(main);
                id = main.getPrjPkid();
            }
        } catch (DataAccessException e) {
            id = null;
        }

        return id;
    }
    
    /**
     * 保存方案破坏类型信息
     * @param phlx 方案破坏类型信息
     * @return 成功返回ServiceConstants.SUCCESS；失败返回ServiceConstants.SUCCESS
     */
    public int savePlanPhlx(TdfkfaBasicXmqntdphlx phlx) {
        
        TdfkfaBasicXmqntdphlxExample example = new TdfkfaBasicXmqntdphlxExample();
        example.createCriteria().andPrjPkidEqualTo(phlx.getPrjPkid());
        try {
            int count = tdfkfaBasicXmqntdphlxDAO.countByExample(example);
            if (count == 0) {
                tdfkfaBasicXmqntdphlxDAO.insertSelective(phlx);
            } else {
                tdfkfaBasicXmqntdphlxDAO.updateByExampleSelective(phlx, example);
            }
        } catch (DataAccessException e) {
            e.printStackTrace();
            return ServiceConstants.FAILURE;
        }
        return ServiceConstants.SUCCESS;
    }
    
    /**
     * 保存方案生产项目概况信息
     * @param xmgk 生产项目概况信息
     * @return 成功返回ServiceConstants.SUCCESS；失败返回ServiceConstants.SUCCESS
     */
    public int savePlanXmgk(TdfkfaBasicScxmgk xmgk) {
        
        TdfkfaBasicScxmgkExample example = new TdfkfaBasicScxmgkExample();
        example.createCriteria().andPrjPkidEqualTo(xmgk.getPrjPkid());
        try {
            int count = tdfkfaBasicScxmgkDAO.countByExample(example);
            if (count == 0) {
                tdfkfaBasicScxmgkDAO.insertSelective(xmgk);
            } else {
                tdfkfaBasicScxmgkDAO.updateByExampleSelective(xmgk, example);
            }
        } catch (DataAccessException e) {
            e.printStackTrace();
            return ServiceConstants.FAILURE;
        }
        return ServiceConstants.SUCCESS;
    }

    /**
     * 保存方案复垦面积信息
     * @param fkmj 复垦面积信息
     * @return 成功返回ServiceConstants.SUCCESS；失败返回ServiceConstants.SUCCESS
     */
    public int savePlanFkmj(TdfkfaBasicYqfkmj fkmj) {
        TdfkfaBasicYqfkmjExample example = new TdfkfaBasicYqfkmjExample();
        example.createCriteria().andPrjPkidEqualTo(fkmj.getPrjPkid());
        try {
            int count = tdfkfaBasicYqfkmjDAO.countByExample(example);
            if (count == 0) {
                tdfkfaBasicYqfkmjDAO.insertSelective(fkmj);
            } else {
                tdfkfaBasicYqfkmjDAO.updateByExampleSelective(fkmj, example);
            }
        } catch (DataAccessException e) {
            e.printStackTrace();
            return ServiceConstants.FAILURE;
        }
        return ServiceConstants.SUCCESS;
    }

    /**
     * 保存方案利用现状信息
     * @param lyxz 利用现状信息
     * @return 成功返回ServiceConstants.SUCCESS；失败返回ServiceConstants.SUCCESS
     */
    public int savePlanLyxz(TdfkfaBasicXmqtdlyxz lyxz) {
        TdfkfaBasicXmqtdlyxzExample example = new TdfkfaBasicXmqtdlyxzExample();
        example.createCriteria().andPrjPkidEqualTo(lyxz.getPrjPkid());
        try {
            int count = tdfkfaBasicXmqtdlyxzDAO.countByExample(example);
            if (count == 0) {
                tdfkfaBasicXmqtdlyxzDAO.insertSelective(lyxz);
            } else {
                tdfkfaBasicXmqtdlyxzDAO.updateByExampleSelective(lyxz, example);
            }
        } catch (DataAccessException e) {
            e.printStackTrace();
            return ServiceConstants.FAILURE;
        }
        return ServiceConstants.SUCCESS;
    }

    /**
     * 保存方案复垦投资信息
     * @param fktz 复垦投资信息
     * @return 成功返回ServiceConstants.SUCCESS；失败返回ServiceConstants.SUCCESS
     */
    public int savePlanFktz(TdfkfaBasicInvest fktz) {
        TdfkfaBasicInvestExample example = new TdfkfaBasicInvestExample();
        example.createCriteria().andPrjPkidEqualTo(fktz.getPrjPkid());
        try {
            int count = tdfkfaBasicInvestDAO.countByExample(example);
            if (count == 0) {
                tdfkfaBasicInvestDAO.insertSelective(fktz);
            } else {
                tdfkfaBasicInvestDAO.updateByExampleSelective(fktz, example);
            }
        } catch (DataAccessException e) {
            e.printStackTrace();
            return ServiceConstants.FAILURE;
        }
        return ServiceConstants.SUCCESS;
    }
    
    /**
     * 保存方案附件信息
     * @param planId 方案ID
     * @param type 附件类型
     * @param url 地址
     * @param fjName 附件名
     * @return 成功返回ServiceConstants.SUCCESS；失败返回ServiceConstants.SUCCESS
     */
    public int saveFafj(Integer planId, String type, String url, String fjName) {
        
        TdfkfaPrjFafj fafj = new TdfkfaPrjFafj();
        fafj.setPlanId(planId);
        fafj.setType(type);
        fafj.setUrl(url);
        fafj.setFjName(fjName);
        
        TdfkfaPrjFafjExample example = new TdfkfaPrjFafjExample();
        example.createCriteria().andPlanIdEqualTo(planId).andTypeEqualTo(type);
        try {
            int count = tdfkfaPrjFafjDAO.countByExample(example);
            if (count == 0) {
                tdfkfaPrjFafjDAO.insertFafj(fafj);
            } else {
                tdfkfaPrjFafjDAO.updateByExampleSelective(fafj, example);
            }
        } catch (DataAccessException e) {
            e.printStackTrace();
            return ServiceConstants.FAILURE;
        }
        return ServiceConstants.SUCCESS;
    }
    
    /**
     * 取得方案附件信息
     * @param planId 方案ID
     * @param type 附件类型
     * @return 方案附件信息
     */
    public List<TdfkfaPrjFafj> loadFafj(Integer planId, String type) {
        TdfkfaPrjFafjExample example = new TdfkfaPrjFafjExample();
        example.createCriteria().andPlanIdEqualTo(planId).andTypeEqualTo(type);
        List<TdfkfaPrjFafj> list = tdfkfaPrjFafjDAO.selectByExample(example);
        return list;
    }

    /**
     * 取得方案所有附件信息
     * @param planId 方案ID
     * @return 方案附件信息
     */
    public List<TdfkfaPrjFafj> loadAllFafj(Integer planId) {
        TdfkfaPrjFafjExample example = new TdfkfaPrjFafjExample();
        example.createCriteria().andPlanIdEqualTo(planId);
        List<TdfkfaPrjFafj> list = tdfkfaPrjFafjDAO.selectByExample(example);
        return list;
    }

    /**
     * 取得用户的方案信息
     * @param username 用户名
     * @return 方案信息
     */
    public TdfkfaMainEx getOwnPlan(String username) {
        TdfkfaUserExample uExample = new TdfkfaUserExample();
        uExample.createCriteria().andUsernameEqualTo(username);
        
        Integer planId = null;
        
        List<TdfkfaUser> list = tdfkfaUserDAO.selectByExample(uExample);
        if (list != null && list.size() != 0 && list.get(0) != null) {
            TdfkfaUser user = list.get(0);
            planId = user.getPlanId();
            if (planId == null) {
                return null;
            }
        } else {
            return null;
        }
        
        TdfkfaMainExample example = new TdfkfaMainExample();
        example.createCriteria().andPrjPkidEqualTo(planId);
        
        TdfkfaMainEx main = tdfkfaMainDAO.selectSinglePlanByExample(example);
        
        return main;
    }
    
    public void deletePlan(Integer planId) {
        if (planId != null) {
            tdfkfaMainDAO.deleteByPrimaryKey(planId);
            tdfkfaBasicInvestDAO.deleteByPrimaryKey(planId);
            tdfkfaBasicScxmgkDAO.deleteByPrimaryKey(planId.toString());
            tdfkfaBasicXmqntdphlxDAO.deleteByPrimaryKey(planId.toString());
            tdfkfaBasicXmqtdlyxzDAO.deleteByPrimaryKey(planId.toString());
            tdfkfaBasicYqfkmjDAO.deleteByPrimaryKey(planId.toString());
        }
    }

    /**
     * 获得典型图片
     * @param planId
     * @return
     */
    public List<TdfkfaPrjDxtp> getPlanDxtp(Integer planId) {
        TdfkfaPrjDxtpExample example = new TdfkfaPrjDxtpExample();
        example.createCriteria().andPlanIdEqualTo(planId);
        List<TdfkfaPrjDxtp> dxtpList = tdfkfaPrjDxtpDAO.selectByExample(example);
        return dxtpList;
    }

    /**
     * 保存方案典型图片信息
     * @param planId 方案ID
     * @param urlList 地址列表
     * @return 成功返回ServiceConstants.SUCCESS；失败返回ServiceConstants.SUCCESS
     */
    public int saveDxtp(Integer planId, List<String> urlList) {

        TdfkfaPrjDxtpExample example = new TdfkfaPrjDxtpExample();
        example.createCriteria().andPlanIdEqualTo(planId);

        try {
            tdfkfaPrjDxtpDAO.deleteByExample(example);
            
            for (String url : urlList) {
                TdfkfaPrjDxtp dxtp = new TdfkfaPrjDxtp();
                dxtp.setPlanId(planId);
                dxtp.setUrl(url);
                tdfkfaPrjDxtpDAO.insertDxtp(dxtp);
            }
        } catch (DataAccessException e) {
            e.printStackTrace();
            return ServiceConstants.FAILURE;
        }
        return ServiceConstants.SUCCESS;
    }

    /**
     * 保存方案编制单位信息
     * @param unit 方案编制单位信息
     * @return 成功返回ServiceConstants.SUCCESS；失败返回ServiceConstants.SUCCESS
     */
    public int savePlanUnit(TdfkfaComposeUnit unit) {
        TdfkfaComposeUnitExample example = new TdfkfaComposeUnitExample();
        example.createCriteria().andPrjPkidEqualTo(unit.getPrjPkid());
        try {
            int count = tdfkfaComposeUnitDAO.countByExample(example);
            if (count == 0) {
                tdfkfaComposeUnitDAO.insertSelective(unit);
            } else {
                tdfkfaComposeUnitDAO.updateByExampleSelective(unit, example);
            }
        } catch (DataAccessException e) {
            e.printStackTrace();
            return ServiceConstants.FAILURE;
        }
        return ServiceConstants.SUCCESS;
    }

    /**
     * 取得方案单位信息
     * @param planId 方案ID
     * @return 方案单位信息
     */
    public TdfkfaComposeUnit getPlanUnit(Integer planId) {
        TdfkfaComposeUnit unit = tdfkfaComposeUnitDAO.selectByPrimaryKey(planId);
        return unit;
    }

    /**
     * 保存评审方案信息
     * @param psyj
     * @param username
     * @return 成功返回ServiceConstants.SUCCESS；失败返回ServiceConstants.SUCCESS
     */
    public int savePlanResult(TdfkfaPrjPsyj psyj, String username) {
        TdfkfaExpertListExample example = new TdfkfaExpertListExample();
        example.createCriteria().andUsernameEqualTo(username);
        List<TdfkfaExpertList> list = tdfkfaExpertListDAO.selectByExampleWithoutBLOBs(example);
        if (list == null || list.size() != 1 || list.get(0) == null) {
            return ServiceConstants.FAILURE;
        }

        TdfkfaPrjExperts prjExperts = tdfkfaPrjExpertsDAO.selectByPrimaryKey(psyj.getPrjPkid());
        if (prjExperts == null) {
            return ServiceConstants.FAILURE;
        }

        TdfkfaMain main = tdfkfaMainDAO.selectByPrimaryKey(psyj.getPrjPkid());
        if (main == null) {
            return ServiceConstants.FAILURE;
        }
        psyj.setPrjName(main.getPrjName());

        Short expertNo = null; 
        Integer expertId = list.get(0).getExpertid();
        if (expertId.equals(prjExperts.getGroupleader())) {
            expertNo = new Short("0");
        }
        if (expertId.equals(prjExperts.getExpert1())) {
            expertNo = new Short("1");
        }
        if (expertId.equals(prjExperts.getExpert2())) {
            expertNo = new Short("2");
        }
        if (expertId.equals(prjExperts.getExpert3())) {
            expertNo = new Short("3");
        }
        if (expertId.equals(prjExperts.getExpert4())) {
            expertNo = new Short("4");
        }
        if (expertId.equals(prjExperts.getExpert5())) {
            expertNo = new Short("5");
        }
        if (expertId.equals(prjExperts.getExpert6())) {
            expertNo = new Short("6");
        }
        if (expertId.equals(prjExperts.getExpert7())) {
            expertNo = new Short("7");
        }
        if (expertId.equals(prjExperts.getExpert8())) {
            expertNo = new Short("8");
        }
        if (expertNo == null) {
            return ServiceConstants.FAILURE;
        }
        
        psyj.setExpertid(expertId);
        psyj.setExpertName(list.get(0).getExpertname());
        psyj.setJudgedate(new Date());
        psyj.setExpertno(expertNo);
        
        TdfkfaPrjPsyjExample psyjExample = new TdfkfaPrjPsyjExample();
        psyjExample.createCriteria().andExpertidEqualTo(psyj.getExpertid()).andPrjPkidEqualTo(psyj.getPrjPkid());
        int myPsyjCount = tdfkfaPrjPsyjDAO.countByExample(psyjExample);
        
        try {
            if (myPsyjCount == 0) {
                tdfkfaPrjPsyjDAO.insertSelective(psyj);
            } else {
                tdfkfaPrjPsyjDAO.updateByExampleSelective(psyj, psyjExample);
            }
        } catch (DataAccessException e) {
            e.printStackTrace();
            return ServiceConstants.FAILURE;
        }
        return ServiceConstants.SUCCESS;
    }

    /**
     * 提交评审方案信息
     * @param planId
     * @param username
     * @return 成功返回ServiceConstants.SUCCESS；失败返回ServiceConstants.FAILURE
     */
    public int submitPlanResult(Integer planId, String username) {

    	TdfkfaExpertListExample example = new TdfkfaExpertListExample();
    	example.createCriteria().andUsernameEqualTo(username);
    	List<TdfkfaExpertList> list = tdfkfaExpertListDAO.selectByExampleWithoutBLOBs(example);

    	if (list == null || list.size() != 1 || list.get(0) == null) {
    		return ServiceConstants.FAILURE;
    	}

    	TdfkfaPrjExperts prjExperts = tdfkfaPrjExpertsDAO.selectByPrimaryKey(planId);
    	
    	if (prjExperts == null) {
    		return ServiceConstants.FAILURE;
    	}
    	
    	Integer expertId = list.get(0).getExpertid();
    	if (expertId.equals(prjExperts.getGroupleader())) {
    		prjExperts.setGroupleaderFlag("1");
    	}
    	if (expertId.equals(prjExperts.getExpert1())) {
    		prjExperts.setExpert1Flag("1");
    	}
    	if (expertId.equals(prjExperts.getExpert2())) {
    		prjExperts.setExpert2Flag("1");
    	}
    	if (expertId.equals(prjExperts.getExpert3())) {
    		prjExperts.setExpert3Flag("1");
    	}
    	if (expertId.equals(prjExperts.getExpert4())) {
    		prjExperts.setExpert4Flag("1");
    	}
    	if (expertId.equals(prjExperts.getExpert5())) {
    		prjExperts.setExpert5Flag("1");
    	}
    	if (expertId.equals(prjExperts.getExpert6())) {
    		prjExperts.setExpert6Flag("1");
    	}
    	if (expertId.equals(prjExperts.getExpert7())) {
    		prjExperts.setExpert7Flag("1");
    	}
    	if (expertId.equals(prjExperts.getExpert8())) {
    		prjExperts.setExpert8Flag("1");
    	}
    	
    	TdfkfaPrjPsyjExample psyjExample = new TdfkfaPrjPsyjExample();
    	psyjExample.createCriteria().andExpertidEqualTo(expertId).andPrjPkidEqualTo(planId);
    	int myPsyjCount = tdfkfaPrjPsyjDAO.countByExample(psyjExample);
    	if (myPsyjCount == 0) {
            return ServiceConstants.FAILURE;
        }
    	try {
	        tdfkfaPrjExpertsDAO.updateByPrimaryKeySelective(prjExperts);

	        int expertNum = 0;
            int flagNum = 0;
            if (prjExperts.getGroupleader() != null) {
                expertNum++;
                if ("1".equals(prjExperts.getGroupleaderFlag())) {
                    flagNum ++;
                }
            }
            if (prjExperts.getExpert1() != null) {
                expertNum++;
                if ("1".equals(prjExperts.getExpert1Flag())) {
                    flagNum ++;
                }
            }
            if (prjExperts.getExpert2() != null) {
                expertNum++;
                if ("1".equals(prjExperts.getExpert2Flag())) {
                    flagNum ++;
                }
            }
            if (prjExperts.getExpert3() != null) {
                expertNum++;
                if ("1".equals(prjExperts.getExpert3Flag())) {
                    flagNum ++;
                }
            }
            if (prjExperts.getExpert4() != null) {
                expertNum++;
                if ("1".equals(prjExperts.getExpert4Flag())) {
                    flagNum ++;
                }
            }
            if (prjExperts.getExpert5() != null) {
                expertNum++;
                if ("1".equals(prjExperts.getExpert5Flag())) {
                    flagNum ++;
                }
            }
            if (prjExperts.getExpert6() != null) {
                expertNum++;
                if ("1".equals(prjExperts.getExpert6Flag())) {
                    flagNum ++;
                }
            }
            if (prjExperts.getExpert7() != null) {
                expertNum++;
                if ("1".equals(prjExperts.getExpert7Flag())) {
                    flagNum ++;
                }
            }
            if (prjExperts.getExpert8() != null) {
                expertNum++;
                if ("1".equals(prjExperts.getExpert8Flag())) {
                    flagNum ++;
                }
            }
            if (expertNum == flagNum) {
                TdfkfaMain m = new TdfkfaMain();
                m.setPrjPkid(planId);
                m.setPrjFaztcode(new Short(DictConstant.FAZT_JUDGED));
                m.setPrjPssj(new Date());
                int ret = tdfkfaMainDAO.updateByPrimaryKeySelective(m);
                if (ret != 1) {
                    return ServiceConstants.FAILURE;
                }
            }
    	} catch (DataAccessException e) {
            e.printStackTrace();
            return ServiceConstants.FAILURE;
        }
        return ServiceConstants.SUCCESS;
	}

	public int setPlanState(Integer planId, String sczt, String fazt) {
		TdfkfaMain main = new TdfkfaMain();
		main.setPrjPkid(planId);
		main.setPrjScztcode(new Short(sczt));
		main.setPrjFaztcode(new Short(fazt));
		try {
			int ret = tdfkfaMainDAO.updateByPrimaryKeySelective(main);
			if (ret != 1) {
				return ServiceConstants.FAILURE;
			}
		} catch (DataAccessException e) {
			return ServiceConstants.FAILURE;
		}
		return ServiceConstants.SUCCESS;
	}

	public int submitPlan(Integer planId) {
		TdfkfaMain main = new TdfkfaMain();
		main.setPrjPkid(planId);
		main.setPrjFaztcode(new Short(DictConstant.FAZT_SUBMITTED));
		try {
			int ret = tdfkfaMainDAO.updateByPrimaryKeySelective(main);
			if (ret != 1) {
				return ServiceConstants.FAILURE;
			}
		} catch (DataAccessException e) {
			return ServiceConstants.FAILURE;
		}
		return ServiceConstants.SUCCESS;
	}

    public int passJudgedPlan(Integer planId, String hpyj) {
        TdfkfaMain main = new TdfkfaMain();
        main.setPrjPkid(planId);
        main.setZxshyj(hpyj);
        main.setPrjPsjgcode(new Short(DictConstant.PSJG_PASSED));
        main.setPrjFaztcode(new Short(DictConstant.FAZT_ZXJUDGED));
        try {
            int ret = tdfkfaMainDAO.updateByPrimaryKeySelective(main);
            if (ret != 1) {
                return ServiceConstants.FAILURE;
            }
        } catch (DataAccessException e) {
            return ServiceConstants.FAILURE;
        }
        return ServiceConstants.SUCCESS;
    }
    
    public int nopassJudgedPlan(Integer planId, String hpyj) {
        TdfkfaMain main = new TdfkfaMain();
        main.setPrjPkid(planId);
        main.setZxshyj(hpyj);
        main.setPrjPsjgcode(new Short(DictConstant.PSJG_NOPASSED));
        main.setPrjFaztcode(new Short(DictConstant.FAZT_ZXJUDGED));
        try {
            int ret = tdfkfaMainDAO.updateByPrimaryKeySelective(main);
            if (ret != 1) {
                return ServiceConstants.FAILURE;
            }
        } catch (DataAccessException e) {
            return ServiceConstants.FAILURE;
        }
        return ServiceConstants.SUCCESS;
    }
    
    public int modifyJudgeJudgedPlan(Integer planId, String hpyj) {
        TdfkfaMain main = new TdfkfaMain();
        main.setPrjPkid(planId);
        main.setZxshyj(hpyj);
        main.setPrjPsjgcode(new Short(DictConstant.PSJG_MODIFY_JUDGE));
        main.setPrjFaztcode(new Short(DictConstant.FAZT_ZXJUDGED));
        try {
            int ret = tdfkfaMainDAO.updateByPrimaryKeySelective(main);
            if (ret != 1) {
                return ServiceConstants.FAILURE;
            }
        } catch (DataAccessException e) {
            return ServiceConstants.FAILURE;
        }
        return ServiceConstants.SUCCESS;
    }

    public int countJudgePassedPlan(PlanListQueryVO queryVO) {
        TdfkfaMainExample example = new TdfkfaMainExample();
        Criteria criteria = example.createCriteria().andPrjPsjgcodeEqualTo(new Short(DictConstant.PSJG_PASSED)).andPrjFaztcodeEqualTo(new Short(DictConstant.FAZT_ZXJUDGED));

        if (!StringUtil.isEmpty(queryVO.getUnit())) {
            criteria.andPrjUnitLike("%".concat(queryVO.getUnit()).concat("%"));
        }
        if (!StringUtil.isEmpty(queryVO.getXzCode())) {
            criteria.andPrjXzcodeEqualTo(new Short(queryVO.getXzCode()));
        }
        if (!StringUtil.isEmpty(queryVO.getKzCode())) {
            criteria.andPrjKzcodeEqualTo(new Short(queryVO.getKzCode()));
            Criteria criteria1 = example.createCriteria().andPrjKzcode1EqualTo(new Short(queryVO.getKzCode())).andPrjPsjgcodeEqualTo(new Short(DictConstant.PSJG_PASSED)).andPrjFaztcodeEqualTo(new Short(DictConstant.FAZT_ZXJUDGED));
            Criteria criteria2 = example.createCriteria().andPrjKzcode2EqualTo(new Short(queryVO.getKzCode())).andPrjPsjgcodeEqualTo(new Short(DictConstant.PSJG_PASSED)).andPrjFaztcodeEqualTo(new Short(DictConstant.FAZT_ZXJUDGED));
            Criteria criteria3 = example.createCriteria().andPrjKzcode3EqualTo(new Short(queryVO.getKzCode())).andPrjPsjgcodeEqualTo(new Short(DictConstant.PSJG_PASSED)).andPrjFaztcodeEqualTo(new Short(DictConstant.FAZT_ZXJUDGED));
            example.or(criteria);
            example.or(criteria1);
            example.or(criteria2);
            example.or(criteria3);
        }

        int count = tdfkfaMainDAO.countByExample(example);

        return count;
    }

    public List<TdfkfaMainEx> getJudgePassedPlanList(PlanListQueryVO queryVO, int intPageIndex, int intPageSize) {
        TdfkfaMainExample example = new TdfkfaMainExample();
        Criteria criteria = example.createCriteria().andPrjPsjgcodeEqualTo(new Short(DictConstant.PSJG_PASSED)).andPrjFaztcodeEqualTo(new Short(DictConstant.FAZT_ZXJUDGED));

        if (!StringUtil.isEmpty(queryVO.getUnit())) {
            criteria.andPrjUnitLike("%".concat(queryVO.getUnit()).concat("%"));
        }
        if (!StringUtil.isEmpty(queryVO.getXzCode())) {
            criteria.andPrjXzcodeEqualTo(new Short(queryVO.getXzCode()));
        }
        if (!StringUtil.isEmpty(queryVO.getKzCode())) {
            criteria.andPrjKzcodeEqualTo(new Short(queryVO.getKzCode()));
            Criteria criteria1 = example.createCriteria().andPrjKzcode1EqualTo(new Short(queryVO.getKzCode())).andPrjPsjgcodeEqualTo(new Short(DictConstant.PSJG_PASSED)).andPrjFaztcodeEqualTo(new Short(DictConstant.FAZT_ZXJUDGED));
            Criteria criteria2 = example.createCriteria().andPrjKzcode2EqualTo(new Short(queryVO.getKzCode())).andPrjPsjgcodeEqualTo(new Short(DictConstant.PSJG_PASSED)).andPrjFaztcodeEqualTo(new Short(DictConstant.FAZT_ZXJUDGED));
            Criteria criteria3 = example.createCriteria().andPrjKzcode3EqualTo(new Short(queryVO.getKzCode())).andPrjPsjgcodeEqualTo(new Short(DictConstant.PSJG_PASSED)).andPrjFaztcodeEqualTo(new Short(DictConstant.FAZT_ZXJUDGED));
            example.or(criteria);
            example.or(criteria1);
            example.or(criteria2);
            example.or(criteria3);
        }
        if (!StringUtil.isEmpty(queryVO.getOrderType())) {
            example.setOrderByClause(queryVO.getOrderType());
        }

        List<TdfkfaMainEx> planList = tdfkfaMainDAO.selectJudgePassedPlanByExample(example, intPageIndex, intPageSize);

        return planList;
    }

    public int passPlan(Integer planId) {
        TdfkfaMain main = new TdfkfaMain();
        main.setPrjPkid(planId);
        main.setPrjPsjgcode(new Short(DictConstant.PSJG_PASSED));
        main.setPrjFaztcode(new Short(DictConstant.FAZT_GBSJUDGED));
        main.setPrjScsj(new Date());
        try {
            int ret = tdfkfaMainDAO.updateByPrimaryKeySelective(main);
            if (ret != 1) {
                return ServiceConstants.FAILURE;
            }
        } catch (DataAccessException e) {
            return ServiceConstants.FAILURE;
        }
        return ServiceConstants.SUCCESS;
    }

    public int nopassPlan(Integer planId) {
        TdfkfaMain main = new TdfkfaMain();
        main.setPrjPkid(planId);
        main.setPrjPsjgcode(new Short(DictConstant.PSJG_NOPASSED));
        main.setPrjFaztcode(new Short(DictConstant.FAZT_GBSJUDGED));
        main.setPrjScsj(new Date());
        try {
            int ret = tdfkfaMainDAO.updateByPrimaryKeySelective(main);
            if (ret != 1) {
                return ServiceConstants.FAILURE;
            }
        } catch (DataAccessException e) {
            return ServiceConstants.FAILURE;
        }
        return ServiceConstants.SUCCESS;
    }

    public int countScPassedPlan(PlanListQueryVO queryVO) {
        TdfkfaMainExample example = new TdfkfaMainExample();
        Criteria criteria = example.createCriteria().andPrjPsjgcodeEqualTo(new Short(DictConstant.PSJG_PASSED)).andPrjFaztcodeEqualTo(new Short(DictConstant.FAZT_GBSJUDGED));

        if (!StringUtil.isEmpty(queryVO.getUnit())) {
            criteria.andPrjUnitLike("%".concat(queryVO.getUnit()).concat("%"));
        }
        if (!StringUtil.isEmpty(queryVO.getXzCode())) {
            criteria.andPrjXzcodeEqualTo(new Short(queryVO.getXzCode()));
        }
        if (!StringUtil.isEmpty(queryVO.getKzCode())) {
            criteria.andPrjKzcodeEqualTo(new Short(queryVO.getKzCode()));
            Criteria criteria1 = example.createCriteria().andPrjKzcode1EqualTo(new Short(queryVO.getKzCode())).andPrjPsjgcodeEqualTo(new Short(DictConstant.PSJG_PASSED)).andPrjFaztcodeEqualTo(new Short(DictConstant.FAZT_GBSJUDGED));
            Criteria criteria2 = example.createCriteria().andPrjKzcode2EqualTo(new Short(queryVO.getKzCode())).andPrjPsjgcodeEqualTo(new Short(DictConstant.PSJG_PASSED)).andPrjFaztcodeEqualTo(new Short(DictConstant.FAZT_GBSJUDGED));
            Criteria criteria3 = example.createCriteria().andPrjKzcode3EqualTo(new Short(queryVO.getKzCode())).andPrjPsjgcodeEqualTo(new Short(DictConstant.PSJG_PASSED)).andPrjFaztcodeEqualTo(new Short(DictConstant.FAZT_GBSJUDGED));
            example.or(criteria);
            example.or(criteria1);
            example.or(criteria2);
            example.or(criteria3);
        }

        int count = tdfkfaMainDAO.countByExample(example);

        return count;
    }

    public List<TdfkfaMainEx> getScPassedPlanList(PlanListQueryVO queryVO, int intPageIndex, int intPageSize) {
        TdfkfaMainExample example = new TdfkfaMainExample();
        Criteria criteria = example.createCriteria().andPrjPsjgcodeEqualTo(new Short(DictConstant.PSJG_PASSED)).andPrjFaztcodeEqualTo(new Short(DictConstant.FAZT_GBSJUDGED));

        if (!StringUtil.isEmpty(queryVO.getUnit())) {
            criteria.andPrjUnitLike("%".concat(queryVO.getUnit()).concat("%"));
        }
        if (!StringUtil.isEmpty(queryVO.getXzCode())) {
            criteria.andPrjXzcodeEqualTo(new Short(queryVO.getXzCode()));
        }
        if (!StringUtil.isEmpty(queryVO.getKzCode())) {
            criteria.andPrjKzcodeEqualTo(new Short(queryVO.getKzCode()));
            Criteria criteria1 = example.createCriteria().andPrjKzcode1EqualTo(new Short(queryVO.getKzCode())).andPrjPsjgcodeEqualTo(new Short(DictConstant.PSJG_PASSED)).andPrjFaztcodeEqualTo(new Short(DictConstant.FAZT_GBSJUDGED));
            Criteria criteria2 = example.createCriteria().andPrjKzcode2EqualTo(new Short(queryVO.getKzCode())).andPrjPsjgcodeEqualTo(new Short(DictConstant.PSJG_PASSED)).andPrjFaztcodeEqualTo(new Short(DictConstant.FAZT_GBSJUDGED));
            Criteria criteria3 = example.createCriteria().andPrjKzcode3EqualTo(new Short(queryVO.getKzCode())).andPrjPsjgcodeEqualTo(new Short(DictConstant.PSJG_PASSED)).andPrjFaztcodeEqualTo(new Short(DictConstant.FAZT_GBSJUDGED));
            example.or(criteria);
            example.or(criteria1);
            example.or(criteria2);
            example.or(criteria3);
        }
        if (!StringUtil.isEmpty(queryVO.getOrderType())) {
            example.setOrderByClause(queryVO.getOrderType());
        }

        List<TdfkfaMainEx> planList = tdfkfaMainDAO.selectJudgePassedPlanByExample(example, intPageIndex, intPageSize);

        return planList;
    }

    public int countScNopassedPlan(PlanListQueryVO queryVO) {
        TdfkfaMainExample example = new TdfkfaMainExample();
        Criteria criteria = example.createCriteria().andPrjPsjgcodeEqualTo(new Short(DictConstant.PSJG_NOPASSED)).andPrjFaztcodeEqualTo(new Short(DictConstant.FAZT_GBSJUDGED));

        if (!StringUtil.isEmpty(queryVO.getUnit())) {
            criteria.andPrjUnitLike("%".concat(queryVO.getUnit()).concat("%"));
        }
        if (!StringUtil.isEmpty(queryVO.getXzCode())) {
            criteria.andPrjXzcodeEqualTo(new Short(queryVO.getXzCode()));
        }
        if (!StringUtil.isEmpty(queryVO.getKzCode())) {
            criteria.andPrjKzcodeEqualTo(new Short(queryVO.getKzCode()));
            Criteria criteria1 = example.createCriteria().andPrjKzcode1EqualTo(new Short(queryVO.getKzCode())).andPrjPsjgcodeEqualTo(new Short(DictConstant.PSJG_NOPASSED)).andPrjFaztcodeEqualTo(new Short(DictConstant.FAZT_GBSJUDGED));
            Criteria criteria2 = example.createCriteria().andPrjKzcode2EqualTo(new Short(queryVO.getKzCode())).andPrjPsjgcodeEqualTo(new Short(DictConstant.PSJG_NOPASSED)).andPrjFaztcodeEqualTo(new Short(DictConstant.FAZT_GBSJUDGED));
            Criteria criteria3 = example.createCriteria().andPrjKzcode3EqualTo(new Short(queryVO.getKzCode())).andPrjPsjgcodeEqualTo(new Short(DictConstant.PSJG_NOPASSED)).andPrjFaztcodeEqualTo(new Short(DictConstant.FAZT_GBSJUDGED));
            example.or(criteria);
            example.or(criteria1);
            example.or(criteria2);
            example.or(criteria3);
        }

        int count = tdfkfaMainDAO.countByExample(example);

        return count;
    }

    public List<TdfkfaMainEx> getScNopassedPlanList(PlanListQueryVO queryVO, int intPageIndex, int intPageSize) {
        TdfkfaMainExample example = new TdfkfaMainExample();
        Criteria criteria = example.createCriteria().andPrjPsjgcodeEqualTo(new Short(DictConstant.PSJG_NOPASSED)).andPrjFaztcodeEqualTo(new Short(DictConstant.FAZT_GBSJUDGED));

        if (!StringUtil.isEmpty(queryVO.getUnit())) {
            criteria.andPrjUnitLike("%".concat(queryVO.getUnit()).concat("%"));
        }
        if (!StringUtil.isEmpty(queryVO.getXzCode())) {
            criteria.andPrjXzcodeEqualTo(new Short(queryVO.getXzCode()));
        }
        if (!StringUtil.isEmpty(queryVO.getKzCode())) {
            criteria.andPrjKzcodeEqualTo(new Short(queryVO.getKzCode()));
            Criteria criteria1 = example.createCriteria().andPrjKzcode1EqualTo(new Short(queryVO.getKzCode())).andPrjPsjgcodeEqualTo(new Short(DictConstant.PSJG_NOPASSED)).andPrjFaztcodeEqualTo(new Short(DictConstant.FAZT_GBSJUDGED));
            Criteria criteria2 = example.createCriteria().andPrjKzcode2EqualTo(new Short(queryVO.getKzCode())).andPrjPsjgcodeEqualTo(new Short(DictConstant.PSJG_NOPASSED)).andPrjFaztcodeEqualTo(new Short(DictConstant.FAZT_GBSJUDGED));
            Criteria criteria3 = example.createCriteria().andPrjKzcode3EqualTo(new Short(queryVO.getKzCode())).andPrjPsjgcodeEqualTo(new Short(DictConstant.PSJG_NOPASSED)).andPrjFaztcodeEqualTo(new Short(DictConstant.FAZT_GBSJUDGED));
            example.or(criteria);
            example.or(criteria1);
            example.or(criteria2);
            example.or(criteria3);
        }
        if (!StringUtil.isEmpty(queryVO.getOrderType())) {
            example.setOrderByClause(queryVO.getOrderType());
        }

        List<TdfkfaMainEx> planList = tdfkfaMainDAO.selectJudgePassedPlanByExample(example, intPageIndex, intPageSize);

        return planList;
    }

    public int zzbb(Integer planId) {
        TdfkfaMain main = new TdfkfaMain();
        main.setPrjPkid(planId);
        main.setZzbb(new Short("1"));
        try {
            int ret = tdfkfaMainDAO.updateByPrimaryKeySelective(main);
            if (ret != 1) {
                return ServiceConstants.FAILURE;
            }
        } catch (DataAccessException e) {
            return ServiceConstants.FAILURE;
        }
        return ServiceConstants.SUCCESS;
    }

    public int unZzbb(Integer planId) {
        TdfkfaMain main = new TdfkfaMain();
        main.setPrjPkid(planId);
        main.setZzbb(new Short("0"));
        try {
            int ret = tdfkfaMainDAO.updateByPrimaryKeySelective(main);
            if (ret != 1) {
                return ServiceConstants.FAILURE;
            }
        } catch (DataAccessException e) {
            return ServiceConstants.FAILURE;
        }
        return ServiceConstants.SUCCESS;
    }

    public void setTdfkfaMainDAO(TdfkfaMainDAO tdfkfaMainDAO) {
        this.tdfkfaMainDAO = tdfkfaMainDAO;
    }

    public void setTdfkfaBasicXmqntdphlxDAO(TdfkfaBasicXmqntdphlxDAO tdfkfaBasicXmqntdphlxDAO) {
        this.tdfkfaBasicXmqntdphlxDAO = tdfkfaBasicXmqntdphlxDAO;
    }

    public void setTdfkfaBasicScxmgkDAO(TdfkfaBasicScxmgkDAO tdfkfaBasicScxmgkDAO) {
        this.tdfkfaBasicScxmgkDAO = tdfkfaBasicScxmgkDAO;
    }

    public void setTdfkfaBasicYqfkmjDAO(TdfkfaBasicYqfkmjDAO tdfkfaBasicYqfkmjDAO) {
        this.tdfkfaBasicYqfkmjDAO = tdfkfaBasicYqfkmjDAO;
    }

    public void setTdfkfaBasicXmqtdlyxzDAO(TdfkfaBasicXmqtdlyxzDAO tdfkfaBasicXmqtdlyxzDAO) {
        this.tdfkfaBasicXmqtdlyxzDAO = tdfkfaBasicXmqtdlyxzDAO;
    }

    public void setTdfkfaBasicInvestDAO(TdfkfaBasicInvestDAO tdfkfaBasicInvestDAO) {
        this.tdfkfaBasicInvestDAO = tdfkfaBasicInvestDAO;
    }

    public void setTdfkfaComposeUnitDAO(TdfkfaComposeUnitDAO tdfkfaComposeUnitDAO) {
        this.tdfkfaComposeUnitDAO = tdfkfaComposeUnitDAO;
    }

    public void setTdfkfaPrjExpertsDAO(TdfkfaPrjExpertsDAO tdfkfaPrjExpertsDAO) {
        this.tdfkfaPrjExpertsDAO = tdfkfaPrjExpertsDAO;
    }

    public void setTdfkfaPrjPsyjDAO(TdfkfaPrjPsyjDAO tdfkfaPrjPsyjDAO) {
        this.tdfkfaPrjPsyjDAO = tdfkfaPrjPsyjDAO;
    }

    public void setTdfkfaPrjFafjDAO(TdfkfaPrjFafjDAO tdfkfaPrjFafjDAO) {
        this.tdfkfaPrjFafjDAO = tdfkfaPrjFafjDAO;
    }

    public void setTdfkfaUserDAO(TdfkfaUserDAO tdfkfaUserDAO) {
        this.tdfkfaUserDAO = tdfkfaUserDAO;
    }

    public void setTdfkfaPrjDxtpDAO(TdfkfaPrjDxtpDAO tdfkfaPrjDxtpDAO) {
        this.tdfkfaPrjDxtpDAO = tdfkfaPrjDxtpDAO;
    }

	public void setTdfkfaExpertListDAO(TdfkfaExpertListDAO tdfkfaExpertListDAO) {
		this.tdfkfaExpertListDAO = tdfkfaExpertListDAO;
	}

}
